#!/bin/bash

. "${HOME}"/.cargo/env

#export RUSTUP_DIST_SERVER="https://rsproxy.cn"
#export RUSTUP_UPDATE_ROOT="https://rsproxy.cn/rustup"

export CARGO_UNSTABLE_SPARSE_REGISTRY=true

# 读取输入参数
C_PROJECT_PATH="/Input"
RUST_OUTPUT_PATH="/Output"
#OPTION_A=$1
#OPTION_B=$2
#PROJECT_NAME=$(basename "$C_PROJECT_PATH")

## 定义输出路径
OUTPUT_BASE="$RUST_OUTPUT_PATH/${PROJECT_NAME}"
rm -rf "$OUTPUT_BASE"
mkdir -p "$OUTPUT_BASE"

rm -rf /app/safer-c2rust/run-results

cp -r -f "${C_PROJECT_PATH}" "$OUTPUT_BASE/P0_original"

# 根据选项A执行不同的转换
case ${OPTION_A} in
#    1)
#        read -p "${OPTION_A}"
#        # 宏处理优化的C到Rust转换
#        python3 convert_c_to_rust.py "$C_PROJECT_PATH" "$OUTPUT_BASE/P1_after_c2rust"
#        cd /app/safer-c2rust
#        python3 run.py -w run-results safer --project "$OUTPUT_BASE/P1_after_c2rust" stat
#
#	      rm -rf "$OUTPUT_BASE/P1_after_c2rust"
#        mv run-results/* "$OUTPUT_BASE"/
#        rm -rf run-results
#        ;;
    1)
        # C2Rust转换
        cd safer-c2rust
        python3 run.py -w run-results c2rust --local_path "$C_PROJECT_PATH" safer stat

        rm -rf "$OUTPUT_BASE/P0_original"
        mv run-results/* "$OUTPUT_BASE"/
        rm -rf run-results
        ;;
    2)
        # 无配置文件转换
        ./without-config-c2rust.sh "$C_PROJECT_PATH" "$OUTPUT_BASE/C2Rust_$PROJECT_NAME"
        mv "$OUTPUT_BASE/C2Rust_$PROJECT_NAME" "$OUTPUT_BASE/P1_after_c2rust"
        mkdir "$OUTPUT_BASE/P1.5_after_wo_config_trans"
        python dup.py "$OUTPUT_BASE/P1_after_c2rust" "$OUTPUT_BASE/P1.5_after_wo_config_trans"
        mv "$OUTPUT_BASE/P1.5_after_wo_config_trans/assert_summary.json" "$OUTPUT_BASE/assert_summary.json"
        # rm -rf "$OUTPUT_BASE/C2Rust"
        # cd /root/readability
        # python main.py "$C_PROJECT_PATH" "$OUTPUT_BASE/P1_after_c2rust" "$OUTPUT_BASE/P5_after_readability"
        cd /app/safer-c2rust
        python run.py -w run-results safer --project "$OUTPUT_BASE/P1.5_after_wo_config_trans" stat
        # python run.py -w run-results safer --project "$OUTPUT_BASE/P5_after_readability" stat
        rm -rf "run-results/P1_after_c2rust"
        mv run-results/* "$OUTPUT_BASE"/
        rm -rf run-results
        ;;
    *)
        echo "Invalid OptionA: $OPTION_A"
        exit 1
        ;;
esac

# 获取P后面的数字最大的文件夹
MAX_FOLDER=""
MAX_NUMBER=-1
for dir in "$OUTPUT_BASE"/P*; do
	if [[ $dir =~ ^$OUTPUT_BASE/P([0-9]+)_ ]]; then
    number=${BASH_REMATCH[1]}
    if [ "$number" -gt "$MAX_NUMBER" ]; then
        MAX_NUMBER=$number
        MAX_FOLDER="$dir"
    fi
  fi
done

#echo "$MAX_FOLDER"

# 根据选项B执行可读性提升优化
if [ "$OPTION_B" == "1" ] && [ -n "$MAX_FOLDER" ]; then
    cd /app/readability
    python main.py "$C_PROJECT_PATH" "$MAX_FOLDER" "$OUTPUT_BASE/P5_after_readability"
    mv "$OUTPUT_BASE/P5_after_readability/readability_summary.json" "$OUTPUT_BASE/readability_summary.json"
fi

# 获取P后面的数字最大的文件夹
MAX_FOLDER=""
MAX_NUMBER=-1
for dir in "$OUTPUT_BASE"/P*; do
	if [[ $dir =~ ^$OUTPUT_BASE/P([0-9]+)_ ]]; then
    number=${BASH_REMATCH[1]}
    if [ "$number" -gt "$MAX_NUMBER" ]; then
        MAX_NUMBER=$number
        MAX_FOLDER="$dir"
    fi
  fi
done

# 根据选项C执行针对cargo test的适配
if [ "$OPTION_C" == "1" ] && [ -n "$MAX_FOLDER" ]; then
    cd /app/formalize_tests
    python main.py "$MAX_FOLDER" "$OUTPUT_BASE/P6_after_test_improve" "test_"
    mv "$OUTPUT_BASE/P6_after_test_improve/test_entry_list.txt" "$OUTPUT_BASE/test_entry_list.txt"
#    cd "$OUTPUT_BASE/P6_after_test_improve"
#    cargo test
    python cargo_test.py "$OUTPUT_BASE/test_entry_list.txt" "$OUTPUT_BASE/P6_after_test_improve"
    mv "$OUTPUT_BASE/P6_after_test_improve/test_output.txt" "$OUTPUT_BASE/test_output.txt"
    mv "$OUTPUT_BASE/P6_after_test_improve/test_summary.json" "$OUTPUT_BASE/test_summary.json"
fi

